package com.glory.hikvisionmobile.customView;

import android.content.Context;
import android.database.DataSetObserver;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.core.util.Pools;

/**
 * *****************************
 *
 * @Anthor yazhou
 * @Email 1184545990@qq.com
 * @Time 2020/3/16 18:33
 * @DESC *****************************
 */
public class HikNoScrollListView extends LinearLayout {

    private Pools.Pool<View> viewPool = new Pools.SynchronizedPool<>(10);

    private BaseAdapter mAdapter;
    private AdapterDataSetObserver mObserver;

    public HikNoScrollListView(Context context) {
        super(context);
    }

    public HikNoScrollListView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public HikNoScrollListView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public HikNoScrollListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public BaseAdapter getAdapter() {
        return mAdapter;
    }

    public void setAdapter(BaseAdapter mAdapter) {
        if (this.mAdapter != null) {
            this.mAdapter.unregisterDataSetObserver(mObserver);
            this.mAdapter = mAdapter;
            this.mAdapter.registerDataSetObserver(mObserver);
            this.mAdapter.notifyDataSetChanged();
        } else {
            this.mAdapter = mAdapter;
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (mAdapter != null) {
            if (mObserver == null) {
                mObserver = new AdapterDataSetObserver();
            }
            mAdapter.registerDataSetObserver(mObserver);
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mAdapter != null) {
            if (mObserver!=null) {
                mAdapter.unregisterDataSetObserver(mObserver);
            }
        }
    }

    class AdapterDataSetObserver extends DataSetObserver {

        @Override
        public void onChanged() {
            super.onChanged();
            for (int i = 0; i < getChildCount(); i++) {
                viewPool.release(getChildAt(i));
            }
            removeAllViewsInLayout();
            for (int i = 0; i < mAdapter.getCount(); i++) {
                View child = mAdapter.getView(i, viewPool.acquire(), HikNoScrollListView.this);
                LayoutParams params = child.getLayoutParams() == null ? new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) : (LayoutParams) child.getLayoutParams();
                addViewInLayout(child, i, params);
            }
            clearFocus();
            requestLayout();
        }

        @Override
        public void onInvalidated() {
            super.onInvalidated();
        }
    }


}
